package main;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.GZIPOutputStream;

public class Main {

	// Cliente
	public static void main(String[] args) throws IOException {
		
		Socket echoSocket = null;
		DataOutputStream out = null;	
		GZIPOutputStream gzipout = null;
		
		try {
			echoSocket = new Socket(InetAddress.getLocalHost() , 8081);
			gzipout = new GZIPOutputStream(echoSocket.getOutputStream());
		
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: taranis.");
			System.exit(1);
		
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for "
					+ "the connection to: taranis.");
			System.exit(1);
		}
		
		out = new DataOutputStream(gzipout);

		CodifMsjaCAP cod = new CodifMsjaCAP();
		
		// Preparo datos a enviar
		Adquisidor_Electro adquisidor = new Adquisidor_Electro();
		adquisidor.empezar_adquisicion_muestras();
				
		Enfermero enf = new Enfermero(35233265, "Juan Perez");
		Paciente  pac = new Paciente();
				
		Mensaje_al_CAP mensj = new Mensaje_al_CAP(123, 456, "Cuerpo de prueba", 
							  InetAddress.getByName("localhost"), enf, pac, 
							  adquisidor.da_electro());
		
		
		byte [] msj = cod.alCable(mensj);
		cod.enmarcar(msj, out);
		gzipout.finish();

		DataInputStream in = new DataInputStream(echoSocket.getInputStream());
		int val = in.readInt();
		
		System.out.print(val);
		
		in.close();
		out.close();
		echoSocket.close();
	}

	
	/*
	 * snippet
	 * 
	 * / Receive the same string back from the server
 		int totalBytesRcvd = 0; // Total bytes received so far
 		int bytesRcvd; // Bytes received in last read
 		while (totalBytesRcvd < data.length) {
 		if ((bytesRcvd = in.read(data, totalBytesRcvd,
 			data.length - totalBytesRcvd)) == -1)
 			throw new SocketException("Connection closed prematurely");
 			totalBytesRcvd += bytesRcvd;
 		} // data array is ful
	 * 
	 * */

	
}
